﻿using System;
using System.Reflection;
using DevExpress.XtraEditors;
using System.Windows.Forms;

namespace QL_Truong_Hoc.DanhMuc
{
    public partial class frmMonHoc : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "danh_muc_dao_tao";
        private bool mblnEditState = false;

        public frmMonHoc()
        {
            InitializeComponent();
        }

        private void frmKhoa_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }
            // TODO: This line of code loads data into the 'qLTHDataSet.mMon' table. You can move, or remove it, as needed.
            this.mMonTableAdapter.Fill(this.qLTHDataSet.mMon);
            xSetMenuState(true);
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            try
            {
                if (e.Button.ButtonType == NavigatorButtonType.Remove)
                {
                    if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn xóa thông tin môn học?")) { e.Handled = true; }
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                dgvMonHoc.CloseEditor();
                if (clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu. Bạn có chắc chắn?"))
                {
                    if (!mblnEditState)
                    {
                        string strMa = clsCommon.fncCnvNullToString(dgvMonHoc.GetFocusedRowCellValue(clmMaMonHoc));
                        if (clsCommon.fncDataExist(this.qLTHDataSet.mMon, "mo_mamon", strMa))
                        {
                            if (!clsCommon.fncMsgConfirm("Mã môn học đã tồn tại. Bạn có chắc muốn tiếp tục?")) return;
                        }
                    }

                    dgvMonHoc.UpdateCurrentRow();
                    xSetMenuState(true);
                    mMonTableAdapter.Update(this.qLTHDataSet.mMon);
                    dgvMonHoc.OptionsBehavior.Editable = false;

                    //raise event update master table
                    Program.gevnApplication.RaiseMasterUpdate();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                dgvMonHoc.CloseEditor();
                dgvMonHoc.AddNewRow();
                dgvMonHoc.OptionsBehavior.Editable = true;
                dgvMonHoc.ShowEditor();
                xSetMenuState(false);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                dgvMonHoc.ShowEditor();
                dgvMonHoc.OptionsBehavior.Editable = true;
                xSetMenuState(false);
                mblnEditState = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (clsCommon.fncMsgConfirm("Bạn có chắc muốn xóa thông tin môn học đang được chọn?"))
                {
                    dgvMonHoc.DeleteSelectedRows();
                    dgvMonHoc.UpdateCurrentRow();
                    mMonTableAdapter.Update(this.qLTHDataSet.mMon);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (clsCommon.fncMsgConfirm("Dữ liệu sẽ không được lưu, bạn có chăc muốn hủy?"))
                {
                    //hủy thay đổi trên grid. lấy lại dữ liệu
                    dgvMonHoc.CancelUpdateCurrentRow();
                    this.mMonTableAdapter.Fill(this.qLTHDataSet.mMon);
                    dgvMonHoc.CloseEditor();
                    xSetMenuState(true);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private bool xSetMenuState(bool blnState)
        {
            bool blnResult = false;
            try
            {
                mniAdd.Enabled = blnState;
                mniEdit.Enabled = blnState;
                mniDelete.Enabled = blnState;
                mniSave.Enabled = !blnState;
                mniCancel.Enabled = !blnState;

                mblnEditState = false;

                return true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }
    }
}